<?php
/**
 * CommonController.class.php - 后台公共控制器
 * @desc 除Public公共访问控制器，其他控制必须继承，用户管理身份及权限认证
 * @package Controller
 * @subpackage null
 * @link http://www.xxx.com
 * @author xxx PHP DEV TEAM JiRY(390066398@qq.com)
 * @copyright 2016-10-28 9:50
 */
namespace Admin\Controller;
use Admin\Controller\BaseController;
class CommonController extends BaseController {
    /**
     * 初始化
     */
    public function _initialize(){
        // 初始化父类
        parent::_initialize();
        
        // 无需签名认证列表
        $not_verify = [];
        
        if(!in_array(strtolower(CONTROLLER_NAME.'_'.ACTION_NAME), $not_verify)){
            // 数据签名认证
            if(!verify_sign(I('post.sign'), I('post.'))){
                $this->ajaxReturnError([
                    'msg'    => L('SIGN_ERROR')
                ]);
            }
        }

        $auth_key = C('AUTH_CONFIG.AUTH_KEY');
        $login_token = session( $auth_key );

        //是否登陆
        if( !$login_token ) {
            $this->ajaxReturnError(L('ADMIN_LOGIN_TIMEOUT'));
        }

        //账号状态实时验证
        $account_status = self::$CommonLogic->getField('AdminInfo', 'data_status', ['id'=>self::$LoginInfo['id']]);
        if((int)$account_status != 1){
            // 删除登陆数据
            self::$CommonLogic->deleteLoginData();

            $this->ajaxReturnError(L('ADMIN_LOGIN_TIMEOUT'));
        }

        // 权限检测
        $Auth = new \Think\Auth();
        $module_name = MODULE_NAME .'/'. CONTROLLER_NAME . '/' . ACTION_NAME;
        //非超级管理员，权限检测
        if(self::$LoginInfo['admin_account'] != C('AUTH_CONFIG.AUTH_ADMINISTRATOR')){
                // 未授权
            if(!$Auth->check( $module_name , $login_token, 3)){
                $this->ajaxReturnError(L('AUTH_NOT'));
                exit;
            }
        }
        
        // 公共数据
        self::common();
    }
    
    /**
     * 公共数据
     */
    private function common(){
        // 默认权限
        $defaule_node_name  = MODULE_NAME . '/' . CONTROLLER_NAME . '/' . ACTION_NAME;
        
        // 权限名称
        $node_name = I('post.node_name', $defaule_node_name, 'trim');
        
        // 拼接应用名称
        if(substr_count($node_name, '/') == 1){
            $node_name = MODULE_NAME . '/' . $node_name;
        }
        
        $pid_path   = self::$CommonLogic->getField('auth_rule', 'pid_path', ['name'=>$node_name]);
        $pid_path_name = get_node_path(['pid_path'=>$pid_path]);
        
        // 公共结果集
        self::$result['path'] = $pid_path_name ? explode(C('PATH_INFO_FLAG'), $pid_path_name) : [];
    }
   
    /**
     * 设置更新节点菜单
     * @return array|boolean 
     */
    protected function flushedNode(){
        $condition  = array(
            'pid'       => 0,
            'status'    => 1,
            //'sys_type'  => 1,       //系统类型；1：平台后台、2：商户后台
            'is_show'   => 1,
        );
        
        $rule_list = self::$CommonLogic->getRows( 'AuthRule', $condition, array('id', 'pid', 'name', 'icon_name', 'title', 'sys_type') , array('sort'=>'asc') );
        
        //总平台菜单
        $sys_main_menu          = [];
        //商家菜单
        $sys_business_menu      = [];
        
        foreach($rule_list as $b_k=>$b_v ){
            //URL地址
            list($module , $controller , $action) = explode('/', $b_v['name']);
        
            if(isset($controller) && isset($action)){
                $rule_list[$b_k]['url'] = $controller.'/'.$action;
            }
        
            $condition['pid'] = $b_v['id'];
            $rule_list[$b_k]['list'] = self::$CommonLogic->getRows( 'AuthRule', $condition, array('id', 'pid', 'name', 'icon_name', 'title', 'sys_type'), array('sort'=>'asc') );
        
            foreach($rule_list[$b_k]['list'] as $s_k=>$s_v ){
        
                //URL地址
                list($module , $controller , $action) = explode('/', $s_v['name']);
        
                if(isset($controller) && isset($action)){
                    $rule_list[$b_k]['list'][$s_k]['url'] = $controller.'/'.$action;
                }
        
                $condition['pid'] = $s_v['id'];
                $rule_list[$b_k]['list'][$s_k]['list'] = self::$CommonLogic->getRows( 'AuthRule', $condition, array('id', 'pid', 'name', 'icon_name', 'title', 'sys_type'), array('sort'=>'asc') );
        
                foreach($rule_list[$b_k]['list'][$s_k]['list'] as $m_k => $m_v){
                    //URL地址
                    list($module , $controller , $action) = explode('/', $m_v['name']);
        
                    if(isset($controller) && isset($action)){
                        $rule_list[$b_k]['list'][$s_k]['list'][$m_k]['url'] = $controller.'/'.$action;
                    }
                }
            }
            
            switch((int)$b_v['sys_type']){
                case 1:
                    array_push($sys_main_menu, $rule_list[$b_k]);
                    break;
                case 2:
                    array_push($sys_business_menu, $rule_list[$b_k]);
                    break;
            }
        }
        
        // 缓冲数据
        //$rs = D('Sys', 'Cache')->getMenu( $sys_main_menu, 'admin');
        //D('Sys', 'Cache')->getMenu( $sys_business_menu, 'business');
        
        // 缓冲数据
        S('sys_menu_admin', $sys_main_menu);
        S('sys_menu_business', $sys_business_menu);
        
        return $sys_main_menu;
    }
}